package cn.nawang.ebeim.client;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.nawang.ebeim.client.bean.BackupTask;
import cn.nawang.ebeim.client.bean.StorageCloud;
import cn.nawang.ebeim.client.bean.StorageServer;
import cn.nawang.ebeim.client.bean.UploadQueue;
import cn.nawang.ebeim.client.exception.ApiRequestException;
import cn.nawang.ebeim.client.util.ApiUtil;

public class UploadThread implements Runnable {
  private static final Logger LOG = LoggerFactory.getLogger(UploadThread.class);

  @Override
  public void run() {
    LOG.debug("upload thread starts at {}", System.currentTimeMillis());
    while (!AppMain.stopFlag.get()) {
      BackupTask task;
      try {
        task = UploadQueue.dequeue();
        upload(task);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

  }

  /** 上传备份任务 */
  public void upload(BackupTask task) {
    LOG.info("upload task: {}", task);
    task.upload();
  }

  // 没有调用
  public List<StorageServer> getStorageServers(StorageCloud sc) {
    return ApiUtil.getStorageServers(sc);
  }

}
